package com.zwcl.glass.tools.controller;

import com.alibaba.fastjson.JSONArray;
import com.zwcl.common.core.domain.entity.ApiResult;
import com.zwcl.common.core.utils.JsonUtils;
import com.zwcl.glass.tools.dto.MapNearDto;
import com.zwcl.glass.tools.entity.ArticleComment;
import com.zwcl.glass.tools.entity.PoiLocation;
import com.zwcl.glass.tools.utils.BaiduMapUtils;
import com.zwcl.glass.tools.utils.MapCountUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/baidu")
public class BaiduMapController {
    /**
     * 添加
     */
    @PostMapping("/near")
    public List<PoiLocation> addArticle(@RequestBody MapNearDto dto) throws Exception {
        JSONArray result= BaiduMapUtils.getNearPoiInfo(dto.getLat().toString(),dto.getLng().toString(),dto.getKeyWords(),dto.getRadius());
        List<PoiLocation> list = new ArrayList<>();
        list= JsonUtils.jsonToList(JsonUtils.objectToJson(result), PoiLocation.class);
        //如果太慢了，则可以单独去读取
        for(PoiLocation item : list){
            Double distance = MapCountUtil.getDistance(dto.getLng(),dto.getLat(),item.getLocation().getLng(),item.getLocation().getLat());
            item.setDistance(distance);
        }
        Collections.sort(list, new Comparator<PoiLocation>() {
            @Override
            public int compare(PoiLocation A1, PoiLocation A2) {
                return A1.getDistance().compareTo(A2.getDistance());
            }
        });
        return list;
    }
}
